package xin.marcher.module.quality.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Repository;
import xin.marcher.module.common.enums.DelFlagEnum;
import xin.marcher.module.common.enums.YesOrNoEnum;
import xin.marcher.module.common.enums.inventory.InventoryExceptionCode;
import xin.marcher.module.common.exception.BaseBizException;
import xin.marcher.module.quality.converter.OperatorCategoryRelationConverter;
import xin.marcher.module.quality.domain.entity.ItemQualityControlSurveyDO;
import xin.marcher.module.quality.domain.entity.OperatorCategoryRelationDO;
import xin.marcher.module.quality.domain.request.ItemQualityControlSurveyTaskRequest;
import xin.marcher.module.quality.mapper.OperatorCategoryRelationMapper;
import xin.marcher.module.rights.support.LoginUser;
import xin.marcher.module.rights.support.LoginUserHolder;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 调查员品类权限资源管理
 */
@Repository
public class OperatorCategoryRelationRepository {

    @Resource
    private OperatorCategoryRelationMapper operatorCategoryRelationMapper;

    @Resource
    private OperatorCategoryRelationConverter operatorCategoryRelationConverter;

    /**
     * 检查调查员是否有权限认领任务
     *
     * @param itemQualityControlSurveyDO
     */
    public void checkOperatorPermission(ItemQualityControlSurveyDO itemQualityControlSurveyDO, ItemQualityControlSurveyTaskRequest request) {
        // 数据所属的品类ID
        Integer categoryId = itemQualityControlSurveyDO.getCategoryId();
        // 获取用户ID
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        if (Objects.isNull(loginUser)) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }
        Long userId = loginUser.getUserId();

        LambdaQueryWrapper<OperatorCategoryRelationDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Objects.nonNull(categoryId), OperatorCategoryRelationDO::getCategoryId, categoryId);
        queryWrapper.eq(Objects.nonNull(userId), OperatorCategoryRelationDO::getUserId, userId);
        queryWrapper.eq(OperatorCategoryRelationDO::getActiveFlag, YesOrNoEnum.YES.getCode());
        queryWrapper.eq(OperatorCategoryRelationDO::getDelFlag, DelFlagEnum.EFFECTIVE.getCode());
        int count = operatorCategoryRelationMapper.selectCount(queryWrapper);

        // 没有权限认领
        if (count <= 0) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

    }

    public List<Integer> queryCategoryIdByUserId() {
        // 获取用户ID
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        if (Objects.isNull(loginUser)) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }
        Long userId = loginUser.getUserId();

        LambdaQueryWrapper<OperatorCategoryRelationDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(OperatorCategoryRelationDO::getUserId, userId);
        queryWrapper.eq(OperatorCategoryRelationDO::getActiveFlag, YesOrNoEnum.YES.getCode());
        queryWrapper.eq(OperatorCategoryRelationDO::getDelFlag, DelFlagEnum.EFFECTIVE.getCode());
        queryWrapper.select(OperatorCategoryRelationDO::getCategoryId);

        List<OperatorCategoryRelationDO> operatorCategoryRelationDOS = operatorCategoryRelationMapper.selectList(queryWrapper);
        // 没有权限
        if (CollectionUtils.isEmpty(operatorCategoryRelationDOS)) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        return operatorCategoryRelationDOS.stream().map(OperatorCategoryRelationDO::getCategoryId).collect(Collectors.toList());
    }
}
